package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/9/22 19:45
 *    R2dbcEntityTemplate模式访问数据库
 */
@RestController
public class UserController {
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/user/{id}")
    public Mono<User> findById(@PathVariable Integer id){
        return userService.findById(id);
    }

    @PostMapping("/user")
    public Mono<Void> addUser(@RequestBody User user) {
        userService.addUser(user);
        return Mono.empty();
    }

    @GetMapping("/users")
    public Flux<User> findAllUser() {
        return userService.findAllUser();
    }

    @GetMapping("/user/namelike")
    public Flux<User> findByNameLike(String name) {
        return userService.findByNameLike(name);
    }

    @GetMapping("/user/nameis")
    public Mono<User> findByNameEquals(String name) {
        return userService.findByNameEquals(name);
    }

    @DeleteMapping("/user/nameis")
    public Mono<Integer> deleteByNameEquals(String name) {
        return userService.deleteByNameEquals(name);
    }

    @PutMapping("/user")
    public Mono<Void> updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return Mono.empty();
    }

    @GetMapping("/user/namelikefirst")
    public Mono<User> findFirstByNameLike(String name) {
        return userService.findFirstByNameLike(name);
    }
}
